var apiHost = wx.getStorageSync("apiHost"); //请求地址
var Appid = wx.getStorageSync('Appid');
var Secret = wx.getStorageSync('Secret');

var AppidFlag = wx.getStorageSync('AppidFlag');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    storeInfo : '',  //出事化店铺信息
    hotGoods :'',    //火爆单品 
    Height: 0,       //初始化高度
    startFlag: 1,              //分页标识   
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var token = wx.getStorageSync('token');             //用户的token信息

    that.setData({
      token: token
    })
    that.getHeight();
    that.initStore(token);      //初始化我的店铺
    that.getStoreGoods(0, 10,token);  //火爆单品
    
  },

  /**
   * 获取 高度
   */
  getHeight: function () {
    var that = this;

    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          Height: res.windowHeight
        });
      }
    });
  },

  /**
   * 初始化我的店铺
   */
  initStore: function (token){
    var that = this;

    wx.showLoading({
      title: '加载中',
      icon: 'loading',
    });

    wx.request({
      url: apiHost + "/mobile/users/yunindex", //仅为示例，并非真实的接口地址
      data: {
        token: token,
        appId: AppidFlag
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        var code = res.data.code;
        var msg = res.data.msg;
        if (code == "10000") {
          var storeInfo = res.data.data
          that.setData({
            storeInfo: storeInfo
          })
        } else {
          that.showMsg(msg);
        }
        wx.hideLoading();   //隐藏加载loading
      }
    })
  },


  /**
  * 下拉
  */
  scrollLower: function () {
    var that = this;
    var start = that.data.startFlag;
    var token = that.data.token;


    that.getStoreGoods(start, 10, token);  //火爆单品
    
  },

  /**
   * 商品分页
   */
  ordersPaging: function (start, hotGoods) {
    var that = this;

    if (start > 1) {
      var hotGoodsList = that.data.hotGoods;
      // 追加数据
      for (var i = 0; i < hotGoods.length; i++) {
        hotGoodsList.push(hotGoods[i])
      }
      start = parseInt(start) + 1;
    } else {
      var hotGoodsList = hotGoods;
      start = 2;
    }
    that.setData({
      hotGoods: hotGoodsList,
      startFlag: start
    })
  },

  /**
   * 火爆单品
   */
  getStoreGoods : function(start,length,token){
    var that = this;

    wx.showLoading({
      title: '加载中',
      icon: 'loading',
    });

    wx.request({
      url: apiHost + "/mobile/users/hostGoods", //仅为示例，并非真实的接口地址
      data: {
        token: token,
        appId: AppidFlag,
        start: start,
        length: length,
        'type' : 'jd,yh' 
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        var code = res.data.code;
        var msg = res.data.msg;
        if (code == "10000") {
          var hotGoods = res.data.data.list
          that.ordersPaging(start,hotGoods);
        } else {
          that.showMsg(msg);
        }
        wx.hideLoading();   //隐藏加载loading
      }
    })
  },

  /**
   * 跳转商品详情
   */
  goGoodsDetail:function(e){
    var that = this;
    var platform = e.currentTarget.dataset.platform;
    var goodsId = e.currentTarget.dataset.id;
    if (platform=='jd'){
      that.goPage("../../../goods/jdGoodsDetail/jdGoodsDetail?id=" + goodsId)
    }
    if (platform == 'yh') {
      that.goPage("../../../goods/yhjGoodsDetail/yhjGoodsDetail?spuid=" + goodsId)
    }
  },

  /**
   * 查看店铺报表
   */
  goReportDetail: function () {
    
    this.goPage("../reportDetail/reportDetail");
  },

  goMyTeam: function () {
    wx.navigateTo({
      url: "../myTeam/myTeam",
    })
  },

  goinvitingF:function(){
    wx.navigateTo({
      url: "../../invitingFriends/invitingFriends",
    })
  },
  goinvitingVip:function(){
    wx.navigateTo({
      url: "../../invitingVIP/invitingVIP",
    })
  },

  goPutForward : function(){
    wx.navigateTo({
      url: "../putForward/putForwardRegin/putForwardRegin",
    })
  },

  /**
   * 跳转页面
   */
  goPage: function (url) {
    wx.showLoading({
      title: '跳转中',
      icon: 'loading',
      mask: true
    })
    wx.navigateTo({
      url: url,
      success: function () {
        wx.hideLoading();
      }
    })
  },

  

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  },
  /**
   * 提示信息
   */
  showMsg: function (msg) {
    wx.showToast({
      title: msg,
      icon: 'none',
      duration: 2000,
      mask: true
    });
  },
})